package edu.uc.ui;

import com.foodview1.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import edu.uc.persistence.UserDAO;

/**
 * Class loginActivity
 * 
 * @author Ckorczyk1
 * 
 * This class contains the code for the buttons on the login screen
 *
 */
public class loginActivity extends MainActivity {

	//(Robert) Declare the login.
	UserDAO login = new UserDAO(context);

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTitle("Food View");
		super.onCreate(savedInstanceState);
		// TODO Auto-generated method stub
		setContentView(R.layout.login);

		// create new Back button object
		Button btnBack = (Button)findViewById(R.id.btnBack);
		// Register the onClick listener
		btnBack.setOnClickListener(new OnClickListener() {

			/**
			 * onClick method
			 */
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent clickLogin = new Intent(v.getContext(), MainActivity.class);
				startActivityForResult(clickLogin, 0);
				finish();
			}
		});

		// create new newUser Button object
		Button btnNewUser = (Button)findViewById(R.id.btnNewUser);
		// Register the onClick listener
		btnNewUser.setOnClickListener(new OnClickListener(){

			/**
			 * onClick method
			 */
			@Override
			public void onClick(View v) {
				//setContentView(R.layout.create_user);
				Intent clickNewUser = new Intent(v.getContext(), CreateUserActivity.class);
				startActivityForResult(clickNewUser, 0);
				finish();
			}

		});

		//  Create New Login button object
		Button btnLogin = (Button)findViewById(R.id.btnLogin);
		final EditText edtUsername = (EditText)findViewById(R.id.tbUsername);
		final EditText edtPassword = (EditText)findViewById(R.id.tbPassword);

		btnLogin.setOnClickListener(new View.OnClickListener() {

			/**
			 * onclick method
			 */
			@Override
			public void onClick(View v) {

				// Convert button objects to strings when button is clicked. (for accuracy)
				final String Username = edtUsername.getText().toString();
				final String Password = edtPassword.getText().toString();
				
				try {
					// Initiate login.fetch from ILogin
					login.logon(Username, Password);

					popup("Success! Logging in as: " + Username + "...");
					
					// Calls main screen (for now) when login is successful
					Intent clickLogin = new Intent(v.getContext(), MainActivity.class);
					startActivityForResult(clickLogin, 0);
					finish();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					popup("Invalid Username / Password!");
					e.printStackTrace();
				}
			}
		});
	}

	// Declare popup message function
	protected void popup(String message){
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onStart()
	 */
	@Override
	public void onStart() {
		super.onStart();
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	public void onResume() {
		super.onResume();
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	public void onPause() {
		super.onPause();
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onStop()
	 */
	@Override
	public void onStop() {
		super.onStop();
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		// TODO Auto-generated method stub

	}

}
